﻿using System.Text;

namespace K9Nano.Updater;

public sealed class UpdaterOptions
{
    /// <summary>
    /// 更新ID
    /// </summary>
    public string ClientId { get; set; }

    /// <summary>
    /// 更新秘钥
    /// </summary>
    public string ClientSecret { get; set; }

    /// <summary>
    /// 目标程序名称，即被更新程序名称
    /// </summary>
    public string MainAppName { get; set; }

    /// <summary>
    /// 目标程序进程名称
    /// </summary>
    public string MainApp { get; set; }

    /// <summary>
    /// 更新服务接口基地址
    /// </summary>
    public string BaseUrl { get; set; }

    /// <summary>
    /// 相对于<see cref="BaseUrl"/>的共用请求路径
    /// </summary>
    public string RequestPath { get; set; } = "/api/k9/";

    /// <summary>
    /// 临时目录
    /// </summary>
    public string TempPath { get; set; }

    /// <summary>
    /// 当前版本
    /// </summary>
    public string Version { get; set; }

    public string ToArguments()
    {
        var prefix = "--";
        var sb = new StringBuilder();

        Append(nameof(BaseUrl), BaseUrl);
        Append(nameof(ClientId), ClientId);
        Append(nameof(ClientSecret), ClientSecret);
        Append(nameof(MainApp), MainApp);
        Append(nameof(MainAppName), MainAppName);
        Append(nameof(RequestPath), RequestPath);
        Append(nameof(TempPath), TempPath);
        Append(nameof(Version), Version);

        return sb.ToString(0, sb.Length - 1);

        void Append(string key, string value)
        {
            sb.Append(prefix);
            sb.Append(Constants.ConfigurationSectionName);
            sb.Append(':');
            sb.Append(key);
            sb.Append("=\"");
            sb.Append(value);
            sb.Append('\"');
            sb.Append(' ');
        }
    }

    public void Valiate()
    {
        if (string.IsNullOrWhiteSpace(ClientId))
        {
            throw new ArgumentNullException(nameof(ClientId));
        }

        if (string.IsNullOrWhiteSpace(BaseUrl))
        {
            throw new ArgumentNullException(nameof(BaseUrl));
        }

        if (string.IsNullOrWhiteSpace(MainApp))
        {
            throw new ArgumentNullException(nameof(MainApp));
        }

        if (string.IsNullOrWhiteSpace(MainAppName))
        {
            throw new ArgumentNullException(nameof(MainAppName));
        }

        if (string.IsNullOrWhiteSpace(RequestPath))
        {
            throw new ArgumentNullException(nameof(RequestPath));
        }

        if (Uri.TryCreate(BaseUrl, UriKind.Absolute, out var uri))
        {
            if (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps)
            {
                throw new ArgumentException("BaseUrl must be http or https");
            }
        }
        else
        {
            throw new ArgumentException("BaseUrl is not a valid uri");
        }

        try
        {
            _ = new Version(Version);
        }
        catch
        {
            throw new ArgumentException("Version is invalid ");
        }
    }
}
